package br.panthro.ui.Background
{
	import br.panthro.media.video.VideoEvent;
	import br.panthro.ui.Background.BackgroundEvent;
	import br.panthro.ui.media.VideoPlayer;
	import br.panthro.utils.loading.AssetLoadEvent;
	import br.panthro.utils.loading.AssetLoader;
	import br.panthro.utils.stage.StageEvent;
	import br.panthro.utils.stage.StageUtil;
	
	import flash.display.Sprite;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.InterpolationMethod;
	import flash.geom.Matrix;
	import flash.utils.setTimeout;
	
	
	public class Background extends Sprite
	{
		private var _backgroundContainer:Sprite = new Sprite();
		private var _videoPlayer:VideoPlayer = new VideoPlayer();
		
		private var _backgroundObject:Object;
		private var _loader:AssetLoader = new AssetLoader();
		private var _stageUtil:StageUtil = new StageUtil();
		
		public function Background()
		{
			super();
		}
		
		public function setProperties(backgroundObject:Object):void
		{
			_backgroundObject = backgroundObject;
			if(backgroundObject.type == "image")
			{
				_loader.load(backgroundObject.contentPath);
				_loader.addEventListener(AssetLoadEvent.LOAD_COMPLETE, loadComplete);
				_loader.addEventListener(AssetLoadEvent.LOAD_ERROR, loadError);
			} else if(backgroundObject.type == "video") {
				_videoPlayer.setVideo(backgroundObject.contentPath, 0x000000, 720, 480, false, true);
				_videoPlayer.addEventListener(VideoEvent.LOAD_COMPLETE, videoLoaded);
				_videoPlayer.addEventListener(VideoEvent.LOAD_ERROR, loadError);
			} else if(backgroundObject.type == "gradient") {	
				drawGradient();
			} else if(backgroundObject.type == "solid") {	
				drawSolid();
			} 
			
		}
		private function drawSolid():void
		{
			var temp:Sprite = new Sprite();
			temp.graphics.beginFill(_backgroundObject.color, 1)
			temp.graphics.drawRect(0,0,_stageUtil.stage.stageWidth,_stageUtil.stage.stageWidth);
			temp.graphics.endFill();
			
			_backgroundContainer.addChild(temp);
			_stageUtil.fitObjectToStage(_backgroundContainer, _backgroundObject.scale);
			setTimeout(backgroundReady, 500);
		}
		private function drawGradient():void
		{
			var temp:Sprite = new Sprite();
			
			
			
			var fillType:String = _backgroundObject.gradientType;
			var colors:Array = [parseInt(_backgroundObject.baseColor), parseInt(_backgroundObject.secondaryColor)];
			var alphas:Array = [1, 1];
			var ratios:Array = [0x00, 0xFF];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(_stageUtil.stage.stageWidth, _stageUtil.stage.stageWidth, (Math.PI/180)*90, 0, 0);
			var spreadMethod:String = SpreadMethod.PAD;
			temp.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod, InterpolationMethod.RGB, _backgroundObject.focalPoint);        
			temp.graphics.drawRect(0,0,_stageUtil.stage.stageWidth,_stageUtil.stage.stageWidth);
			temp.graphics.endFill();
			
			
			_backgroundContainer.addChild(temp);
			_stageUtil.fitObjectToStage(_backgroundContainer, _backgroundObject.scale);
			setTimeout(backgroundReady, 500);
			
		}
		
		private function videoLoaded(event:VideoEvent):void
		{
			_backgroundContainer.addChild(_videoPlayer);
			setTimeout(backgroundReady, 500);
		}
		
		private function loadComplete(event:AssetLoadEvent):void
		{
			_backgroundContainer.addChild(_loader.content);
			backgroundReady();
		}
		private function backgroundReady():void
		{
			_stageUtil.stage.addEventListener(StageEvent.RESIZE, stageResize);
			backgroundAspect(_backgroundContainer);
			dispatchEvent(new BackgroundEvent(BackgroundEvent.READY));
			
		}
		private function stageResize(event:StageEvent):void
		{
			backgroundAspect(_backgroundContainer);
		}
		public function backgroundAspect(target:Sprite):void
		{
			_stageUtil.fitObjectToStage(_backgroundContainer, _backgroundObject.scale);
			_stageUtil.alignObjectToStage(_backgroundContainer, _backgroundObject.alignment);	
		}
		private function loadError(event:AssetLoadEvent):void
		{
			trace("error loading bg");
		}
		
		public function get content():Sprite
		{
			return _backgroundContainer;
		}
	}
}